package cn.javazd.xml;

import java.io.File;
import java.io.FileOutputStream;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class JDom2Xml implements Xml {

	@Override
	public void createXml() {
		long startTime = System.currentTimeMillis();
		try {
			Element app = new Element("app");
			Document document = new Document(app);

			Element iphone = new Element("iphone");
			iphone.setAttribute("type", "IOS 11.1");
			app.addContent(iphone);

			Element version = new Element("version");
			version.setText("1.0");
			Element name = new Element("name");
			name.setText("iphone 8");
			Element color = new Element("color");
			color.setText("磨砂黑");
			iphone.addContent(version);
			iphone.addContent(name);
			iphone.addContent(color);

			Format format = Format.getCompactFormat();
			format.setEncoding("UTF-8");
			format.setIndent("\t");

			XMLOutputter outputer = new XMLOutputter(format);
			File file = new File("JDom2-app.xml");
			outputer.output(document, new FileOutputStream(file));
			System.out.print("create JDom2 xml success ==== ");
			System.out.println("speed times: " + (System.currentTimeMillis() - startTime));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
